package com.naiterui.ehp.bs.patient.controller.inner;

import com.naiterui.ehp.bp.bo.patient.FamilyBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientFamilyService;
import com.naiterui.ehp.bs.patient.vo.family.FamilyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 家庭成员相关内部接口
 *
 * @since 1.0.0
 */
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.FAMILY})
@RequestMapping("ap/inner/family")
@Controller
public class ApInnerFamilyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApInnerFamilyController.class);
    @Autowired
    private IPatientFamilyService patientFamilyService;

    /**
     * 获取家庭成员里列表
     * @param familyIds
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseVO<List<FamilyBO>> list(@RequestBody List<Long> familyIds) throws BusinessException {
        LOGGER.info("获取家庭成员里列表: familyIds={}", familyIds);
        return PublicService.returnResponseVO(patientFamilyService.getDrFamilyList(familyIds));
    }

    /**
     * 根据id获取家庭成员信息
     * InnerFamilyController.getOrder()
     *
     * @param familyId
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2018年6月5日
     * @since 2.18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseVO<FamilyBO> detail(Long familyId) throws BusinessException {
        if (familyId == null) {
            LOGGER.error("获取家庭成员内部接口参数错误，familyId: {}", familyId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        FamilyBO bo = patientFamilyService.getInnerFamilyById(familyId);
        return PublicService.returnResponseVO(bo);
    }

    /**
     * 判断家庭成员关系（姓名、性别、年龄）
     * InnerFamilyController.judgeFamily()
     *
     * @param name
     * @param gender
     * @param age
     * @param ageUnit
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年6月27日
     * @since 1.0.0
     */
    @RequestMapping("/judgeFamily")
    @ResponseBody
    public ResponseVO<Integer> judgeFamily(Long patientId, String name, Integer gender, Integer age, String ageUnit) throws BusinessException {
        LOGGER.info("判断家庭成员关系,patientId:{},name:{},gender:{},age:{},ageUnit:{}", patientId, name, gender, age, ageUnit);
        Integer relation = patientFamilyService.judgeFamily(patientId, name, gender, age, ageUnit);
        return PublicService.returnResponseVO(relation);
    }

    /**
     * 判断是否本人
     * InnerFamilyController.judgeOneself()
     *
     * @param patientId
     * @param name
     * @param gender
     * @param age
     * @param ageUnit
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年7月19日
     * @return1:是本人：2不是本人：3:患者没有填写记录
     * @since 1.0.0
     */
    @RequestMapping("/judgeOneself")
    @ResponseBody
    public ResponseVO<Integer> judgeOneself(Long patientId, String name, Integer gender, Integer age, String ageUnit) throws BusinessException {
        LOGGER.info("判断是否本人,patientId:{},name:{},gender:{},age:{},ageUnit:{}", patientId, name, gender, age, ageUnit);
        Integer flag = patientFamilyService.judgeOneself(patientId, name, gender, age, ageUnit);
        return PublicService.returnResponseVO(flag);
    }

    /**
     * 添加或修改本人信息
     * InnerFamilyController.addSelf()
     *
     * @param patientId
     * @param gender
     * @param name
     * @param phone
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2018年9月6日
     * @since 2.24
     */
    @RequestMapping(value = "/addUpdSelf", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加或修改本人信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<FamilyBO> addUpdSelf(Long patientId, Integer gender, String birth, String phone, String name) throws BusinessException {
        LOGGER.info("添加或修改本人信息，patientId: {}, gender: {}, birth: {}，phone:{},name：{}", patientId, gender, birth, phone, name);
        if (patientId == null || gender == null || StringUtils.isEmpty(birth) || StringUtils.isEmpty(name)) {
            LOGGER.error("添加或修改本人信息内部接口参数错误，patientId: {}, gender: {}, birth: {}，name：{}", patientId, gender, birth, name);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        FamilyBO bo = patientFamilyService.insertOrUpdateSelf(patientId, gender, birth, phone, name);
        return PublicService.returnResponseVO(bo);
    }

    /**
     * 获取本人信息
     * InnerFamilyController.getSelf()
     *
     * @param patientId
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2018年9月12日
     * @since 2.24
     */
    @RequestMapping(value = "/getSelf", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取本人信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<FamilyBO> getSelf(Long patientId) throws BusinessException {
        if (patientId == null) {
            LOGGER.error("获取本人信息内部接口参数错误，patientId: {}", patientId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        FamilyVO selfInfo = patientFamilyService.saveAndGetSelfInfo(patientId);
        FamilyBO bo = new FamilyBO();
        BeanUtils.copyProperties(selfInfo, bo);
        return PublicService.returnResponseVO(bo);
    }
}
